package org.example.k8s_cloudplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.entity.PodAccess;
import org.example.k8s_cloudplatform.entity.res.Result;
import org.example.k8s_cloudplatform.service.PodAccessService;
import org.example.k8s_cloudplatform.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/podAccess")
public class PodAccessController {
    @Resource
    private PodAccessService podAccessService;

    @GetMapping("/viewUnauthorizedUser")
    public Result viewUnauthorizedUser(@RequestHeader("Token") String token,@RequestParam Integer dockerID, @RequestParam(defaultValue = "") String search) {
        Integer ownerID= JwtUtil.IDParser(token);
        return podAccessService.viewUnauthorizedUser(ownerID, dockerID, search);
    }

    @GetMapping("/addAuthorizedUser")
    public Result addAuthorizedUser(@RequestHeader("Token") String token,@RequestParam Integer dockerID, @RequestParam Integer userID) {
        Integer ownerID= JwtUtil.IDParser(token);
        return podAccessService.addAuthorizedUser(ownerID, dockerID, userID);
    }

    @GetMapping("/viewPodAuthorization")
    public Result viewPodAuthorization(@RequestParam Integer dockerID) {
        LambdaQueryWrapper<PodAccess> podAccessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        podAccessLambdaQueryWrapper.eq(PodAccess::getDockerID, dockerID);
        return Result.success(podAccessService.list(podAccessLambdaQueryWrapper));
    }


    @GetMapping("/deleteAuthorizedUser")
    public Result deleteAuthorizedUser(@RequestParam Integer ID) {
        return podAccessService.deleteAuthorizedUser(ID);
    }
}
